package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86131
 * Date: 2024-02-20
 * Time: 15:42
 */
public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书");
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入你要新增图书的书名:");
        String name = scanner.nextLine();

        System.out.println("请输入你要新增图书的作者:");
        String author = scanner.nextLine();

        System.out.println("请输入你要新增图书的类型:");
        String type = scanner.nextLine();

        System.out.println("请输入你要新增图书的价格:");
        int price = scanner.nextInt();

        //根据以上信息生成一个书的对象
        Book book = new Book(name, author, price, type);

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book tmp = bookList.getBook(i);
            if (tmp.getName().equals(name)) {
                System.out.println("存在这本书,不能重复添加!");
                return;
            }
        }
        //没有重复的书,可以添加
        bookList.setBook(book,currentSize);
        bookList.setUsedSize(currentSize+1);
    }
}
